package com.example.fiction.ui.activity.reader;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.SeekBar;

import com.example.fiction.BR;
import com.example.fiction.R;
import com.example.fiction.base.BaseRecyclerViewAdapter;
import com.example.fiction.databinding.ActivityReaderBinding;
import com.example.fiction.ui.activity.home.HomeActivity;
import com.example.fiction.ui.activity.reader.entry.ChapterItemBean;
import com.example.fiction.utils.PermissionUtils;
import com.example.fiction.utils.ScreenUtils;
import com.example.fiction.utils.StatusBarUtil;

import me.goldze.mvvmhabit.base.BaseActivity;

/**
 * 小说阅读器
 * auth: 姚鸿杰
 * time: 2019/3/24 0:34
 **/
public class ReaderActivity extends BaseActivity<ActivityReaderBinding, ReaderViewModel> {

    @Override
    public int initContentView(Bundle bundle) {
        return R.layout.activity_reader;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public ReaderViewModel initViewModel() {
        //View持有ViewModel的引用，如果没有特殊业务处理，这个方法可以不重写
        return ViewModelProviders.of(this).get(ReaderViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();

        Intent intent = getIntent();
        String bookId = intent.getStringExtra("bookId");    //获取书名

        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtil.setRootViewFitsSystemWindows(this, false);
        //设置状态栏透明
        StatusBarUtil.setTranslucentStatus(this);

        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtil.setStatusBarDarkTheme(this, true)) {
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtil.setStatusBarColor(this, 0x55000000);
        }

        viewModel.setActivity(this);

        //最总逻辑处理到viewModel中
        viewModel.setReaderView(binding.readerView);
        //设置系统分割线
        binding.readerDirectory.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        viewModel.setReaderDirectory(binding.readerDirectory, new BaseRecyclerViewAdapter<ChapterItemBean>(this, getLayoutInflater(),
                R.layout.item_directory, BR.directory, null));
        viewModel.setReaderDrawLayout(binding.readerDrawLayout);
        viewModel.setNavigation(binding.navigation);
        viewModel.setReaderMenu(binding.readerMenu);
        viewModel.setSeekBar((SeekBar) findViewById(R.id.chapter_seek_bar));
        viewModel.initData(bookId);

        //目录点击事件
        findViewById(R.id.reader_catalogue).setOnClickListener(viewModel.openChapter);
        //上一章
        findViewById(R.id.text_prev_chapter).setOnClickListener(viewModel.preChapter);
        //下一章
        findViewById(R.id.text_next_chapter).setOnClickListener(viewModel.nextChapter);
        //加大字体
        findViewById(R.id.bigFont).setOnClickListener(viewModel.biggerFont);
        findViewById(R.id.smallFont).setOnClickListener(viewModel.smallerFont);
        //字体
        viewModel.openChapter(binding.readerFontContainer, findViewById(R.id.readerFont));
        //切换翻页模式
        viewModel.setEffect(findViewById(R.id.upDown), 0);
        viewModel.setEffect(findViewById(R.id.leftRight), 1);
        viewModel.setEffect(findViewById(R.id.singleSimulation), 2);
        viewModel.setEffect(findViewById(R.id.doubleSimulation), 3);
        //亮度
        viewModel.openBtightness(binding.readerBrgn, findViewById(R.id.btightness));
        //调节亮度
        SeekBar regulaeLight = findViewById(R.id.light);
        regulaeLight.setOnSeekBarChangeListener(viewModel.regulationLight);
        //系统亮度
        findViewById(R.id.systemLight).setOnClickListener(viewModel.setSystemLight);
        //切换背景
        viewModel.switchBackground(findViewById(R.id.reader_bg_0), 0);
        viewModel.switchBackground(findViewById(R.id.reader_bg_1), 1);
        viewModel.switchBackground(findViewById(R.id.reader_bg_2), 2);
        viewModel.switchBackground(findViewById(R.id.reader_bg_3), 3);
    }


    private long mDownTime;
    private float mDownX;

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int longClickTime = ViewConfiguration.getLongPressTimeout();
        int touchSlop = ViewConfiguration.get(this).getScaledPagingTouchSlop();
        int screenWidth = ScreenUtils.getScreenWidth(this);
        int screenHeight = ScreenUtils.getScreenHeight(this);
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (ev.getX() > screenWidth / 3 && ev.getX() < screenWidth * 2 / 3
                        && ev.getY() > screenHeight / 3 && ev.getY() < screenHeight * 2 / 3) {
                    mDownTime = System.currentTimeMillis();
                } else {
                    mDownTime = 0;
                }
                mDownX = ev.getX();
                break;
            case MotionEvent.ACTION_UP:
                if (binding.readerDrawLayout.isDrawerOpen(binding.navigation)) {
                    break;  //防止出现目录时点击冲突
                }
                if (System.currentTimeMillis() - mDownTime < longClickTime && (Math.abs(ev.getX() - mDownX) < touchSlop)) {
                    if (!binding.readerMenu.isShowing() && !binding.readerDrawLayout.isDrawerOpen(binding.navigation)
                            && !binding.readerFontContainer.isShowing() && !binding.readerBrgn.isShowing()) {
                        binding.readerMenu.show();
                    } else {
                        binding.readerMenu.dismiss();
                        binding.readerFontContainer.dismiss();
                        binding.readerBrgn.dismiss();
                    }
                    return true;
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (binding.readerMenu.isShowing() || binding.readerDrawLayout.isDrawerOpen(binding.navigation)
                    || binding.readerFontContainer.isShowing() || binding.readerBrgn.isShowing()) {
                binding.readerMenu.dismiss();
                binding.readerFontContainer.dismiss();
                binding.readerBrgn.dismiss();
                binding.readerDrawLayout.closeDrawers();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
